<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备编号" prop="clientNo">
        <el-input
          v-model="queryParams.clientNo"
          placeholder="请输入设备编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="事件类型" prop="eventType">
        <el-select v-model="queryParams.eventType" placeholder="请选择事件类型" clearable size="small">
          <el-option
            v-for="dict in dict.type.beidou_event_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="报警时间">
        <el-date-picker
          v-model="daterangeEventTime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['device:beidouEvent:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['device:beidouEvent:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="beidouEventList" :default-sort="defaultSort" @sort-change="onSortChange" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="数据编号" align="center" prop="id" />
      <el-table-column label="设备编号" align="center" prop="clientNo" />
      <el-table-column label="事件类型" align="center" prop="eventType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.beidou_event_type" :value="scope.row.eventType"/>
        </template>
      </el-table-column>
      <el-table-column label="事件ID" align="center" prop="eventId" />
      <el-table-column sortable="custom" label="最大报警值" align="center" prop="maxValue">
        <template slot-scope="scope">
          <el-statistic group-separator="," :precision="2" decimal-separator="." :value="scope.row.maxValue">
            <template slot="suffix">
              <span v-if="scope.row.eventType == 'Ii'">度</span>
              <span v-else-if="scope.row.eventType == 'angle'">°</span>
              <span v-else-if="scope.row.eventType == 'position'">mm</span>
            </template>
          </el-statistic>
        </template>
      </el-table-column>
      <el-table-column label="最大报警类型" align="center" prop="maxValueType">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.maxValueType == 'positionX'" type="danger">X轴位移</el-tag>
          <el-tag v-else-if="scope.row.maxValueType == 'positionY'" type="danger">Y轴位移</el-tag>
          <el-tag v-else-if="scope.row.maxValueType == 'positionZ'" type="danger">Z轴位移</el-tag>
          <el-tag v-else-if="scope.row.maxValueType == 'angleX'" type="warning">X轴倾斜</el-tag>
          <el-tag v-else-if="scope.row.maxValueType == 'angleY'" type="warning">Y轴倾斜</el-tag>
          <el-tag v-else-if="scope.row.maxValueType == 'angleZ'" type="warning">Z轴倾斜</el-tag>
          <el-tag v-else-if="scope.row.maxValueType == 'Ii'" type="primary">震动</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="报警数据" align="center" prop="originData">
        <template slot-scope="scope">
          <el-popover
            placement="top-start"
            title="报警数据"
            width="200"
            trigger="hover"
            :content="analysisOriginData(scope.row.originData)">
            <span slot="reference">{{ scope.row.originData }}</span>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column sortable="custom" label="报警时间" align="center" prop="eventTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.eventTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['device:beidouEvent:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listBeidouEvent, delBeidouEvent } from "@/api/device/beidouEvent";

export default {
  name: "BeidouEvent",
  dicts: ['beidou_event_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 海岛北斗事件表格数据
      beidouEventList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 备注时间范围
      daterangeEventTime: [],
      // 默认排序
      defaultSort: {prop: 'eventTime', order: 'descending'},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        clientNo: null,
        eventType: null,
        eventId: null,
        maxValue: null,
        maxValueType: null,
        remark: null,
        params:{},
        orderByColumn: 'eventTime',
        isAsc: 'descending'
      }
    };
  },
  created() {
  },
  activated() {
    const beginEventTime = this.$route.query.beginEventTime;
    const endEventTime = this.$route.query.endEventTime;
    if (null != beginEventTime && '' != beginEventTime && null != endEventTime && '' != endEventTime) {
      this.daterangeEventTime = [beginEventTime, endEventTime];
      this.queryParams.params["beginEventTime"] = beginEventTime;
      this.queryParams.params["endEventTime"] = beginEventTime;
    }else{
      this.queryParams.params = {};
      this.daterangeEventTime = [];
    }
    this.getList();
  },
  methods: {
    analysisOriginData(content){
      if(!content){
        return content;
      }
      let result = content.replaceAll("panid", "设备ID").replaceAll("position", "位移")
                          .replaceAll("angle", "倾斜").replaceAll("Ii", "震动")
                          .replaceAll("X", "X轴").replaceAll("Y", "Y轴")
                          .replaceAll("Z", "Z轴").replaceAll("event_ID", "事件ID");
      return result;
    },
    onSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },
    /** 查询海岛北斗事件列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeEventTime && '' != this.daterangeEventTime) {
        this.queryParams.params["beginEventTime"] = this.daterangeEventTime[0];
        this.queryParams.params["endEventTime"] = this.daterangeEventTime[1];
      }
      listBeidouEvent(this.queryParams).then(response => {
        this.beidouEventList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeEventTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除海岛北斗事件编号为"' + ids + '"的数据项？').then(function() {
        return delBeidouEvent(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('device/beidouEvent/export', {
        ...this.queryParams
      }, `beidouEvent_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
